<template>
	<view>
		<!--不要公共模块-->
		<!--isFullScreen 可以实其全屏显示-->
		<view class="container">
			<image class="bg-img" src="../static/login.jpeg"></image>
			<view class="content"></view>
		</view>
		<view class="column">
			<button class="button" type="primary" @click="weixin">微信一键登录</button>
			<view style="height: 40rpx;"></view>
			<button class="button" type="primary" @click="regist">其他用户登录或注册</button>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {

			};
		},

		methods: {
			weixin() {
				wx.login({
					desc: '用于完善用户资料',
					success: (res) => {
						console.log("获取成功", res);
						uni.request({
							url: "http://localhost:8081/user/loginByWX?code=" + res.code,
							method: "GET",
						}).then(result => {
							console.log(result[1].data.data);
							if (result[1].data.code == 200) {
								uni.getUserProfile({
									desc: "用于完善用户信息",
									success: (res1) => {
										getApp().globalData.userWechatInfo = res1.userInfo;
										console.log(res1)
										uni.showToast({
											icon: "none",
											title: '获取成功'
										})
									},
									fail: (err) => {
										console.log(err)
										uni.showToast({
											icon: "none",
											title: '用户拒绝获取'
										})
								 }
								});
								uni.showToast({
									title: "登录成功",
									icon: "success",
									duration: 1000
								});
								getApp().globalData.username = result[1].data.data.uname;
								getApp().globalData.password = result[1].data.data.upassword;
								getApp().globalData.job = result[1].data.data.ujob;
								getApp().globalData.phone = result[1].data.data.uphone;
								getApp().globalData.level = result[1].data.data.utype;
								getApp().globalData.id = result[1].data.data.uid;
								if (result[1].data.data.utype == 1)
								{
									console.log("???");
										setTimeout(function() {
										    uni.redirectTo({
										    	url:"my/my"
										    })
										}, 1000);
								}
				
								else if (result[1].data.data.ulevel == 2)
									setTimeout(function() {
									    uni.navigateTo({
									    url:'main'
											})
									}, 1000);
								else if (result[1].data.data.ulevel == 3)
								;
							}
							else if (result[1].data.code == 500) {
								uni.showToast({
									icon: "error",
									title: "系统出错"
								})
								return;
							} else if (result[1].data.code == 400) {
								console.log("???");
								uni.showToast({
									icon: "error",
									title: "用户未绑定微信"
								})
							}
						})
					},
					fail: (err) => {
						uni.showToast({
							title: "获取失败",
							icon: "error"
						})
					}
				});
			},
			regist() {
				uni.navigateTo({
					url:"normal/login"
				})
			},
		},

		onReady() {},
	};
</script>

<style lang="scss" scoped>
	.bg-img {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.column {		
		position: relative;
		// padding: 5px;
		top: 1000rpx;
	}
	.button{
		width: 90%;
		background-color: #55aa7f;
	}
</style>
